package com.example.eeuse;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.nio.charset.Charset;
import java.util.*;
import java.util.stream.Collectors;

public class CreateSampleData {

    public static void main(String[] args) throws Exception {
        String dateStr="2024-01-22";
        createCount(dateStr);
        //createData(dateStr);
    }

    private static void createData(String dateStr) throws Exception {
        List<String> list=FileUtil.readLines("D:\\test\\count.txt", "utf-8");
        Date beginDate= DateUtil.parse(dateStr,"yyyy-MM-dd");
        Calendar searchTime = Calendar.getInstance();
        searchTime.setTime(beginDate);
        for(int i=0;i<2;i++) {
            String date=DateUtil.format(searchTime.getTime(), "yyyy-MM-dd");
            for(int j=0;j<20;j++) {
                KibanaSample.writeSampleByDate(date,list.get(j).substring(0,3),list.get(j).substring(4,7) );
            }
            searchTime.add(Calendar.DATE, 1);
        }

    }
    public static void createCount(String dateStr)
    {
        Date beginDate= DateUtil.parse(dateStr,"yyyy-MM-dd");
        Calendar searchTime = Calendar.getInstance();
        searchTime.setTime(beginDate);
        Map<String,Integer> resultMap=new HashMap<>();
        for(int i=0;i<1;i++) {
            String date=DateUtil.format(searchTime.getTime(), "yyyy-MM-dd");
            Map<String,Integer> tempMap=KibanaGet.getSampleMap(date);
            tempMap.forEach((k,v)->{
                Integer val= resultMap.computeIfAbsent(k,nv->0);
                resultMap.put(k,v+val);
            });
            searchTime.add(Calendar.DATE, 1);
        }
        List<String> list = resultMap.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).map(e -> e.getKey()+":"+e.getValue()).collect(Collectors.toList());
        FileUtil.writeLines(list,"D:\\test\\count.txt","utf-8");
    }
}
